<?php
include 'php/config.php';
session_start();
if (!isset($_SESSION["id_utente"])) {
	header("Location: " . $ISSUER_HOST . "/login.php");
	exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<script src="http://localhost:8888/issuer.js"></script>
<script src="js/upload_badges.js"></script>
<title>Open Badges - Pagina Personale </title> 
</head>
<body>
	<h1>Open Badges - Elenco Badges</h1>
	<div>
	<p>Cambio password: <a href="change_password_page.php">Cambia</a></p>
	</div>
	<div id="message"></div>
	<?php if (!$_SESSION["admin"]) { ?>
	<div>
	<p>Vengono qui presentate le badge assegnate a <?php echo $_SESSION["email"]; ?></p>
	</div>
	<?php 
		// Connecting, selecting database
		$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
		if ($mysqli->connect_errno) {
			echo "<p>Failed to connect to MySQL: " . $mysqli->connect_error . "<p>";
		} else {
			$query = sprintf($EARNED_BADGES, $_SESSION["id_utente"]);
			$result = $mysqli->query($query);
			if (!$result) {
				echo "<p>Failed to run query " . $query . ": (" . $mysqli->errno . ") " .
						$mysqli->error . "<p>";
			} else {
				$urls = array();
				$count = 0;
				while ($row = $result->fetch_assoc()) {
					echo "<div>";
					echo "<img src=\"" . $row['immagine'] . "\"  height=\"100\" width=\"100\">";
					if ($row["evidence"])
						echo "<p><a href=\"" . $row["evidence"] . "\">Lavoro eseguito</a></p>";
					if ($row["expires"])
						echo "<p>La badge ha validità fino al giorno: " . date('d-m-Y', $row["expires"]) . "</p>";
					if ($row['uploaded'] == FALSE) {
						$singleUrl = "'" . $row["verifyUrl"] . "'";
						echo sprintf($UPLOAD_BUTTON, "", $singleUrl, $_SESSION['id_utente']);
				 		$urls[] = $singleUrl;
				 		$count++;
					}	
					echo "</div>";
				}
				
				if ($count != 0) {
					echo "<div>";
					echo sprintf($UPLOAD_BUTTON, " tutte", implode(',', $urls), $_SESSION['id_utente']);
					echo "</div>";
				}
			}
			// Free result set
			mysqli_free_result($result);
			
			// Closing connection
			$mysqli->close();
			
		}
	} // end if (!$_SESSION["admin"])
?>
<div>
	<p>Torna alla <a href="index.html">Home</a></p>
</div>
</body>
</html>
